package com.maxlen.gateway.filter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.maxlen.common.constant.LoginConstant;
import com.maxlen.common.constant.MemberConstant;
import com.maxlen.common.utils.JwtUtils;
import com.maxlen.common.utils.R;
import com.maxlen.common.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.List;

@Order(-1)
@Component
public class RequestInterceptorFilter implements GlobalFilter {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        try {

            HttpHeaders headers = exchange.getRequest().getHeaders();


    //        redisTemplate.opsForValue().get();

    //        List<String> whiteList = new ArrayList<>();
    //        whiteList.add("/admin/login");
    //        whiteList.add("/reception/login");
    //        whiteList.add("/reception/register");
    //        ServerHttpRequest request = exchange.getRequest();
    //        HttpHeaders headers = request.getHeaders();
    //        RequestPath path = request.getPath();
    //        String value = path.pathWithinApplication().value();
    //        for (String var : whiteList) {
    //            if(var.equals(value)){
    //                return chain.filter(exchange);
    //            }
    //        }
            List<String> tokenList = headers.get(LoginConstant.TOKEN);
            if(tokenList != null){
                String token = tokenList.get(0);
                if(JwtUtils.checkToken(token)){
                    String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(token);
                    String key = MemberConstant.MEMBER_LOGIN + memberIdByJwtToken;
                    if(redisTemplate.hasKey(key)){
                        return chain.filter(exchange);
                    }
                }
            }else{
                return chain.filter(exchange);
            }

            ServerHttpResponse response = exchange.getResponse();
            response.getHeaders().set("content-type","application/json;charset=utf-8");
            DataBufferFactory bufferFactory = response.bufferFactory();
            ObjectMapper objectMapper = new ObjectMapper();

            DataBuffer wrap = bufferFactory.wrap(objectMapper.writeValueAsBytes(R.error(ResultCode.USER_ACCOUNT_EXPIRED)));

            return response.writeWith(Mono.fromSupplier(() -> wrap));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
